using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class pages_CustomBooking : System.Web.UI.Page
{
    CustomBooking cb = new CustomBooking();
    protected void Page_Load(object sender, EventArgs e)
    {
        int fLocId = 0,sLocId=0, facId=0;
        if(Request["f_loc"]!=null) fLocId=int.Parse(Request["f_loc"]);
        if (Request["s_loc"] != null) sLocId = int.Parse(Request["s_loc"]);
        if (Request["fac_id"] != null) facId = int.Parse(Request["fac_id"]);
        LocationDAO daoLoc = new LocationDAO();   
        FacilityDAO daoFac=new FacilityDAO();
        Location fLoc = daoLoc.GetLocation(fLocId);
        Location sLoc = daoLoc.GetLocation(sLocId);
        cb.DeparturePlace = fLoc;
        cb.ArrivalPlace = sLoc;
        cb.Facility = daoFac.GetFacility(facId);
        Session["c_booking"] = cb;
        lblFirstPlace.Text = fLoc.Name;
        lblSecondPlace.Text = sLoc.Name;
        lblFacility.Text = cb.Facility.Name;
        uctPersonInfo.LoadData();
    }
    protected void lnkAccept_Click(object sender, EventArgs e)
    {
        Validate();
        if (!IsValid) return;
        GetPersonInfo();

        User user=Session["user"] as User;
        if (IsRegisteredUser(user))
        {
            AddBookingForRegisteredUser(user);
        }
        else
        {
            user.PersonInfor.ModifiedDate = DateTime.Now;
            AddBookingForUnregisterUser(user.PersonInfor);
        }

    }
    /// <summary>
    /// Add tour booking for registered customer
    /// </summary>
    /// <param name="userId"></param>
    public void AddBookingForRegisteredUser(User user)
    {
        BookingDAO daoBooking = new BookingDAO();
        CustomBookingDAO daoCustomBooking = new CustomBookingDAO();

        
        cb.CustomerID = user.PersonInfor.Id;
        cb.DepartureTime = DateTime.Parse(txtDepDate.Text);
        cb.GuestNumber = int.Parse(txtGuestNum.Text);        
        cb.Type = 3; //custom booking
        cb.ModifiedDate = DateTime.Now;
        cb.ModifiedBy = user.Id;
        if (daoBooking.Insert(cb) > 0)
        {
            if (daoCustomBooking.Insert(cb) > 0)
            {
                Session["p_booking"] = cb;
                Response.Redirect("CheckoutCustomBooking.aspx");
            }
            else lblMessage.Text = "Sorry! Some errors has ocurred in our system.<br/> Please try later or contact us now";
        }
        else lblMessage.Text = "Sorry! Some errors has ocurred in our system.<br/> Please try later or contact us now";

    }
    public void AddBookingForUnregisterUser(Person perInfo)
    {
        PersonDAO daoPerson = new PersonDAO();
        BookingDAO daoBooking = new BookingDAO();
        CustomBookingDAO daoCustomBooking = new CustomBookingDAO();

        cb.DepartureTime = DateTime.Parse(txtDepDate.Text);
        cb.GuestNumber = int.Parse(txtGuestNum.Text);
        cb.Note = txtNote.Text;
        cb.Type = 3; //custom booking
        if (daoPerson.Insert(perInfo) <= 0)
        {
            lblMessage.Text = "Error";
            return;
        }
        cb.ModifiedDate = DateTime.Now;
        cb.CustomerID = perInfo.Id;        
        if (daoBooking.Insert(cb) <= 0)
        {
            lblMessage.Text = "Error when create new booking";
            return;
        }
        if (daoCustomBooking.Insert(cb) <= 0)
        {
            lblMessage.Text = "Error when trying to create package booking";
            return;
        }
        Session["c_booking"] = cb;
        Response.Redirect("CheckoutCustomBooking.aspx");
    }
    //Get personal information in registration form
    public void GetPersonInfo()
    {
        User user = Session["user"] as User;
        if (user == null) user = new User();
        user.PersonInfor.FirstName = uctPersonInfo.FirstName;
        user.PersonInfor.LastName = uctPersonInfo.LastName;
        user.PersonInfor.DateOfBirth = uctPersonInfo.DateOfBirth;
        user.PersonInfor.Email = uctPersonInfo.Email;
        user.PersonInfor.Gender = uctPersonInfo.Gender;
        user.PersonInfor.Phone = uctPersonInfo.Phone;
        user.PersonInfor.Country = uctPersonInfo.Country;
        user.PersonInfor.State = uctPersonInfo.State;
        user.PersonInfor.City = uctPersonInfo.City;
        user.PersonInfor.Address = uctPersonInfo.Address;
        Session["user"] = user;
    }
    public bool IsRegisteredUser(User user)
    {
        return user.Id > 0;
    }
}
